package com.ruoyi.drone.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.drone.domain.DroneTaskApplication;
import com.ruoyi.drone.service.IDroneTaskApplicationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 任务接单Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/drone/application")
public class DroneTaskApplicationController extends BaseController
{
    @Autowired
    private IDroneTaskApplicationService droneTaskApplicationService;

    /**
     * 查询任务接单列表
     */
    @PreAuthorize("@ss.hasPermi('drone:application:list')")
    @GetMapping("/list")
    public TableDataInfo list(DroneTaskApplication droneTaskApplication)
    {
        startPage();
        List<DroneTaskApplication> list = droneTaskApplicationService.selectDroneTaskApplicationList(droneTaskApplication);
        return getDataTable(list);
    }

    /**
     * 根据任务ID查询任务接单列表
     */
    @PreAuthorize("@ss.hasPermi('drone:application:list')")
    @GetMapping("/listByTaskId/{taskId}")
    public TableDataInfo listByTaskId(@PathVariable("taskId") Long taskId)
    {
        startPage();
        List<DroneTaskApplication> list = droneTaskApplicationService.selectDroneTaskApplicationListByTaskId(taskId);
        return getDataTable(list);
    }

    /**
     * 根据飞手ID查询任务接单列表
     */
    @PreAuthorize("@ss.hasPermi('drone:application:list')")
    @GetMapping("/listByPilotId/{pilotId}")
    public TableDataInfo listByPilotId(@PathVariable("pilotId") Long pilotId)
    {
        startPage();
        List<DroneTaskApplication> list = droneTaskApplicationService.selectDroneTaskApplicationListByPilotId(pilotId);
        return getDataTable(list);
    }

    /**
     * 导出任务接单列表
     */
    @PreAuthorize("@ss.hasPermi('drone:application:export')")
    @Log(title = "任务接单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DroneTaskApplication droneTaskApplication)
    {
        List<DroneTaskApplication> list = droneTaskApplicationService.selectDroneTaskApplicationList(droneTaskApplication);
        ExcelUtil<DroneTaskApplication> util = new ExcelUtil<DroneTaskApplication>(DroneTaskApplication.class);
        util.exportExcel(response, list, "任务接单数据");
    }

    /**
     * 获取任务接单详细信息
     */
    @PreAuthorize("@ss.hasPermi('drone:application:query')")
    @GetMapping(value = "/{applicationId}")
    public AjaxResult getInfo(@PathVariable("applicationId") Long applicationId)
    {
        return success(droneTaskApplicationService.selectDroneTaskApplicationByApplicationId(applicationId));
    }

    /**
     * 根据任务ID和飞手ID获取任务接单信息
     */
    @PreAuthorize("@ss.hasPermi('drone:application:query')")
    @GetMapping(value = "/getByTaskAndPilot/{taskId}/{pilotId}")
    public AjaxResult getByTaskAndPilot(@PathVariable("taskId") Long taskId, @PathVariable("pilotId") Long pilotId)
    {
        return success(droneTaskApplicationService.selectDroneTaskApplicationByTaskIdAndPilotId(taskId, pilotId));
    }

    /**
     * 新增任务接单
     */
    @PreAuthorize("@ss.hasPermi('drone:application:add')")
    @Log(title = "任务接单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DroneTaskApplication droneTaskApplication)
    {
        return toAjax(droneTaskApplicationService.insertDroneTaskApplication(droneTaskApplication));
    }

    /**
     * 飞手申请任务
     */
    @PreAuthorize("@ss.hasPermi('drone:application:apply')")
    @Log(title = "申请任务", businessType = BusinessType.INSERT)
    @PostMapping("/apply")
    public AjaxResult applyTask(@RequestBody DroneTaskApplication droneTaskApplication)
    {
        try {
            return toAjax(droneTaskApplicationService.applyTask(droneTaskApplication));
        } catch (RuntimeException e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改任务接单
     */
    @PreAuthorize("@ss.hasPermi('drone:application:edit')")
    @Log(title = "任务接单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DroneTaskApplication droneTaskApplication)
    {
        return toAjax(droneTaskApplicationService.updateDroneTaskApplication(droneTaskApplication));
    }

    /**
     * 审核任务接单（通过）
     */
    @PreAuthorize("@ss.hasPermi('drone:application:audit')")
    @Log(title = "审核任务接单", businessType = BusinessType.UPDATE)
    @PutMapping("/approve/{applicationId}")
    public AjaxResult approveApplication(@PathVariable("applicationId") Long applicationId)
    {
        String auditBy = getUsername();
        return toAjax(droneTaskApplicationService.approveApplication(applicationId, auditBy));
    }

    /**
     * 审核任务接单（拒绝）
     */
    @PreAuthorize("@ss.hasPermi('drone:application:audit')")
    @Log(title = "审核任务接单", businessType = BusinessType.UPDATE)
    @PutMapping("/reject/{applicationId}")
    public AjaxResult rejectApplication(@PathVariable("applicationId") Long applicationId, @RequestBody DroneTaskApplication application)
    {
        String auditBy = getUsername();
        String rejectReason = application.getRejectReason();
        return toAjax(droneTaskApplicationService.rejectApplication(applicationId, rejectReason, auditBy));
    }

    /**
     * 取消任务申请
     */
    @PreAuthorize("@ss.hasPermi('drone:application:cancel')")
    @Log(title = "取消任务申请", businessType = BusinessType.UPDATE)
    @PutMapping("/cancel/{applicationId}")
    public AjaxResult cancelApplication(@PathVariable("applicationId") Long applicationId)
    {
        return toAjax(droneTaskApplicationService.cancelApplication(applicationId));
    }

    /**
     * 删除任务接单
     */
    @PreAuthorize("@ss.hasPermi('drone:application:remove')")
    @Log(title = "任务接单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{applicationIds}")
    public AjaxResult remove(@PathVariable Long[] applicationIds)
    {
        return toAjax(droneTaskApplicationService.deleteDroneTaskApplicationByApplicationIds(applicationIds));
    }

    /**
     * 检查任务申请是否唯一
     */
    @PreAuthorize("@ss.hasPermi('drone:application:query')")
    @GetMapping("/checkUnique/{taskId}/{pilotId}")
    public AjaxResult checkUnique(@PathVariable("taskId") Long taskId, @PathVariable("pilotId") Long pilotId)
    {
        boolean isUnique = droneTaskApplicationService.checkTaskApplicationUnique(taskId, pilotId);
        return success(isUnique);
    }
}
